package com.example.app2024.CustomView;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app2024.CustomView.colorText.ColorTrackTextView;
import com.example.app2024.CustomView.jibuqi.QQStepView;
import com.example.app2024.CustomView.myprogress.ProgressBar;
import com.example.app2024.CustomView.myprogress.ShapeView;
import com.example.app2024.R;

public class CurstomViewActivity extends AppCompatActivity
{

      private ProgressBar mProgressBar;
      private ShapeView mShapeView;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            setContentView(R.layout.activity_curstom_view);
            ViewCompat.setOnApplyWindowInsetsListener(
                    findViewById(R.id.main), (v, insets) -> {
                          Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                          v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                          return insets;
                    });

            View view = findViewById(R.id.id_pb);
            view.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        ObjectAnimator.ofInt(view, "progress", 0, 100).setDuration(3000).start();
                  }
            });


            mShapeView = (ShapeView) findViewById(R.id.shape_view);
            //            ======================================进度条
            mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
            mProgressBar.setMax(4000);

            ValueAnimator animator = ObjectAnimator.ofFloat(0, 4000);
            animator.setDuration(7000);
            animator.start();
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
            {
                  @Override
                  public void onAnimationUpdate(ValueAnimator animation)
                  {
                        float progress = (float) animation.getAnimatedValue();
                        mProgressBar.setProgress((int) progress);
                  }
            });


            //=====================================步数器代码
            final QQStepView qqStepView = (QQStepView) findViewById(R.id.step_view);
            qqStepView.setStepMax(4000);

            // 属性动画 后面讲的内容
            ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, 3000);
            valueAnimator.setDuration(8000);
            valueAnimator.setInterpolator(new DecelerateInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
            {
                  @Override
                  public void onAnimationUpdate(ValueAnimator animation)
                  {
                        float currentStep = (float) animation.getAnimatedValue();
                        qqStepView.setCurrentStep((int) currentStep);
                  }
            });
            valueAnimator.start();

      }


      public void exchange(View view)
      {
            new Thread(new Runnable()
            {
                  @Override
                  public void run()
                  {
                        while (true)
                        {

                              runOnUiThread(new Runnable()  //诸 , 我感觉这里有问题
                              {
                                    @Override
                                    public void run()
                                    {
                                          mShapeView.exchange();  //这是控件的方法, 不是界面上按钮调用的方法,所以不是死循环
                                    }
                              });

                              try
                              {
                                    Thread.sleep(2000);
                              } catch (InterruptedException e)
                              {
                                    throw new RuntimeException(e);
                              }

                        }
                  }
            }).start();
      }

      public void dochangeTextview(View view)
      {
            ColorTrackTextView textView = findViewById(R.id.mytextview);
            textView.setDirection(ColorTrackTextView.Direction.RIGHT_TO_LEFT);


            // 属性动画 后面讲的内容
            ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, 1f);
            valueAnimator.setDuration(8000);
            valueAnimator.setInterpolator(new DecelerateInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
            {
                  @Override
                  public void onAnimationUpdate(ValueAnimator animation)
                  {
                        float currentStep = (float) animation.getAnimatedValue();
                        textView.setCurrentProgress(currentStep);
                  }
            });
            valueAnimator.start();
      }
}